define(function (require, exports, module) {
    require("resource/js/easyui/themes/icon.css");
    require("resource/js/easyui/easyloader.js");
    require.async("resource/js/component/rules.js");
    var easyutil = require("resource/js/component/easyutil.js");
    var AJAX = require("resource/js/component/ajax.js");
    var config = {
        name: "水稳搅拌机设备设置",
        column: [[
            {field: 'waterStationId', title: '拌合站ID', width: 50, align: 'center', height: 50, hidden: true},
            {field: 'deviceName', title: '拌合站名称', width: 50, align: 'center', height: 50},
            {
                field: 'codeNum',
                title: '编码',
                width: 70,
                align: 'center',
                height: 50,
                formatter: function (value, row) {
                    var val = row.projectNo + "-" + row.sectionNo + "-" + row.stationNo;
                    return val;
                }
            },
            {field: 'frd', title: 'RFID设备', width: 70, align: 'center', height: 50},
            {field: 'sn', title: 'SN', width: 70, align: 'center', height: 50},
            {field: 'plannedTotal', title: '计划总产量(吨)', width: 70, align: 'center', height: 50}
        ]
        ]
    }

    var dgutil = undefined;
    var datagridconfig = undefined;

    //初始化页面
    function initPage() {
    	var sectionId = sessionStorage.getItem('sectionId');  //获取标段id
        if(sectionId ==  null){
            sectionId = 0;
        }
		$("#searchSectionId").val(sectionId);
        dgutil = new easyutil.ajaxDataGrid(datagridconfig);
    }

    //新增拌合站
    var deviceaddDialogInit = function (baseUrl, row) {
        $("#deviceadddlg").dialog({
            iconCls: "icon-save",
            title: "新增拌合站",
            href: baseUrl,
            buttons: [{
                text: "保存",
                iconCls: "icon-ok",
                accessKey: {akey: 18, mkey: 83},
                handler: function () {
                    using("form", function () {
                        if ($("#addform").form("validate")) {
                        	var sectionId = sessionStorage.getItem('sectionId');  //获取标段id
                        	if(sectionId == null || sectionId == undefined){
								sectionId = '001';
							}
                        	var obj = $("#addform").serializeJson();
							obj.sectionId = sectionId;
							obj.codeNum = obj.projectNo+"-"+obj.sectionNo+"-"+obj.stationNo;
                            easyutil.ajaxHandler(BASEPATH + "/dcs/water/station/saveWaterStationModel.htm", obj, function () {
                                dgutil.pager();
                                $("#deviceadddlg").dialog("close");
                            });

                        }
                    });
                }
            },
                {
                    text: '取消',
                    addClass: 'l-btn-red', iconCls: 'icon-not',
                    accessKey: {akey: 18, mkey: 67},
                    handler: function () {
                        $("#deviceadddlg").dialog('close');
                    }
                }
            ]
        });
        $("#deviceadddlg").dialog("open");
    }

    //修改拌合站
    var deviceupdDialogInit = function (baseUrl, row) {
        $("#deviceadddlg").dialog({
            iconCls: "icon-save",
            title: "修改",
            href: baseUrl,
            onLoad: function () {
                $('#updform').form('load', row);
            },
            buttons: [{
                text: "保存",
                iconCls: "icon-ok",
                accessKey: {akey: 18, mkey: 83},
                handler: function () {
                    using("form", function () {
                        if ($("#updform").form("validate")) {
                        	var sectionId = sessionStorage.getItem('sectionId');  //获取标段id
                        	if(sectionId == null || sectionId == undefined){
								sectionId = '001';
							}
                        	var obj = $("#updform").serializeJson();
							obj.sectionId = sectionId;
							obj.codeNum = obj.projectNo+"-"+obj.sectionNo+"-"+obj.stationNo;
                            easyutil.ajaxHandler(BASEPATH + "/dcs/water/station/updateWaterStationModel.htm", obj, function () {
                                dgutil.pager();
                                $("#deviceadddlg").dialog("close");
                            });
                        }
                    });
                }
            },
                {
                    text: '取消',
                    addClass: 'l-btn-red', iconCls: 'icon-not',
                    accessKey: {akey: 18, mkey: 67},
                    handler: function () {
                        $("#deviceadddlg").dialog("close");
                    }
                }
            ]
        });
        $("#deviceadddlg").dialog("open");
    }

    //拌合参数设置
    function toParamDialogInit(baseUrl, row) {
        $("#paramdlg").dialog({
            href: baseUrl,
            onLoad: function () {
            }
        });
        $("#searchform").form("load",row);
		$("#stationId_search").val(row.waterStationId);
        $("#paramdlg").dialog("open");
    }

    //删除拌合站
    var delInit = function (baseUrl, row) {
        //水稳拌合站
        AJAX.init().success(function(code,msg,data){
            if(data > 0){
                $.messager.show({ title:"系统提示", msg:"请解绑拌合站下设备", timeout:2000, showType:'slide' });
            }else{
                easyutil.ajaxHandler(baseUrl, row, function () {
                    initPage();
                });
            }
        }).error(function(code,msg,data){
        }).post(BASEPATH+'/dcs/water/station/queryWaterStationDel.htm?blenderId='+row.waterStationId);

    }

    exports.trans = function () {
        var buttons = {
            title: {
                _class: "",
                text: "设备管理"
            },
            btn: [
//                  {
//                iconCls: "icon-search",
//                text: "查询",
//                addClass: "l-btn-display",
//                url: "/dcs/water/station/queryStationByPage.htm",
//                handler: function () {
//                    dgutil.pager(1);
//                }
//            },
                {
                    iconCls: "icon-add",
                    text: "添加",
                    /*addClass:"l-btn-display",*/
                    url: "/view/dcs/water/device/deviceadd",
                    handler: deviceaddDialogInit
                },
                {
                    iconCls: "icon-edit",
                    text: "修改",
                    /*addClass:"l-btn-display",*/
                    url: "/view/dcs/water/device/deviceupd",
                    needRow: true,
                    handler: deviceupdDialogInit
                },
                {
                    iconCls: 'icon-remove',
                    url: '/dcs/water/station/deleteStationModel.htm',
                    text: "删除",
                    /*addClass:'l-btn-display',*/
                    needRow: true,
                    isCheck: true,
                    handler: delInit
                },
                {
                    iconCls: "icon-edit",
                    text: "拌合参数设置",
                    /*addClass:"l-btn-display",*/
                    url: "/view/dcs/water/device/deviceParamters",
                    needRow: true,
                    handler: toParamDialogInit
                }
            ]
        };
        datagridconfig = {
            fitColumns: true,
            singleSelect: false,
            striped: true,
            autoRowHeight: false,
            fit: true,
            url: BASEPATH + "/dcs/water/station/queryStationByPage.htm",
            toolbar: buttons,
            columns: config.column,
            eid: "dg",
            args: "searchform"
        };
        using(["datagrid", "parser", "textbox", "numberbox", "dialog", "combobox", "datetimebox", "validatebox", "messager"], function () {
            initPage();
        });
    }
});


